package com.spic.business.plan.supplies.controller;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.common.core.utils.poi.ExcelMsg;
import com.spic.business.plan.supplies.domain.PlanExcelDto;
import com.spic.business.plan.supplies.domain.PlanSupplies;
import com.spic.business.plan.supplies.service.IPlanSuppliesService;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
 * 应急物资台账Controller
 *
 * @author spic
 * @date 2021-10-27
 */
@Slf4j
@RestController
@RequestMapping("plan/supplies")
public class PlanSuppliesController extends BaseController
{
    @Autowired
    private IPlanSuppliesService planSuppliesService;

    /**
     * 查询应急物资台账列表
     */
    @PreAuthorize(hasPermi = "business/plan:supplies:list")
    @GetMapping("/list")
    public TableDataInfo list(PlanSupplies planSupplies)
    {
        startPage();
        List<PlanSupplies> list = planSuppliesService.selectPlanSuppliesList(planSupplies);
        return getDataTable(list);
    }

    /**
     * 导出应急物资台账列表
     */
    @PreAuthorize(hasPermi = "business/plan:supplies:export")
    @Log(title = "应急物资台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlanSupplies planSupplies) throws IOException
    {
        List<PlanSupplies> list = planSuppliesService.selectPlanSuppliesList(planSupplies);
        ExcelUtil<PlanSupplies> util = new ExcelUtil<PlanSupplies>(PlanSupplies.class);
        util.exportExcel(response, list, "应急物资台账数据");
    }

    /**
     * 获取应急物资台账详细信息
     */
    @PreAuthorize(hasPermi = "business/plan:supplies:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(planSuppliesService.selectPlanSuppliesById(id));
    }

    /**
     * 新增应急物资台账
     */
    @PreAuthorize(hasPermi = "business/plan:supplies:add")
    @Log(title = "应急物资台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlanSupplies planSupplies)
    {

        return toAjax(planSuppliesService.insertPlanSupplies(planSupplies));
    }

    /**
     * 修改应急物资台账
     */
    @PreAuthorize(hasPermi = "business/plan:supplies:edit")
    @Log(title = "应急物资台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlanSupplies planSupplies)
    {

        return toAjax(planSuppliesService.updatePlanSupplies(planSupplies));
    }

    /**
     * 删除应急物资台账
     */
    @PreAuthorize(hasPermi = "business/plan:supplies:remove")
    @Log(title = "应急物资台账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return toAjax(planSuppliesService.deletePlanSuppliesByIds(ids));
    }

    /**
     * 批量导入校验
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PreAuthorize(hasPermi = "business/plan:comunication:edit")
    @Log(title = "物资批量导入校验", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult validateData(MultipartFile file, boolean updateSupport) throws Exception
    {
        BusinessExcelUtil<PlanExcelDto> util = new BusinessExcelUtil<>(PlanExcelDto.class);
        List<PlanExcelDto> PlanSuppliesDtoList = util.importExcel(file.getInputStream());
        ExcelMsg msg = util.validateData(PlanSuppliesDtoList);
        if(msg.hasError()) return AjaxResult.error("物资批量导入校验失败",msg);
        msg = planSuppliesService.importData(PlanSuppliesDtoList, updateSupport);
        return AjaxResult.success(msg);
    }


    /**
     * 下载导入模板
     * @return
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<PlanExcelDto> util = new BusinessExcelUtil<>(PlanExcelDto.class);
        util.importTemplateExcel(response, "应急物资模板");
    }
}
